﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PatentFee.Entity;

namespace PatentFee.Bussines
{
    /// <summary>
    /// 缴费信息操作类。
    /// </summary>
    public class NeedPayFeeInfoBussines
    {
        /// <summary>
        /// 新增信息。
        /// </summary>
        /// <param name="needPayFeeInfo">缴费信息。</param>
        public void Add(NeedPayFeeInfo needPayFeeInfo)
        {
            using (var dbContext = new PatentFeeDbContext())
            {
                dbContext.NeedPayFeeInfos.Add(needPayFeeInfo);
                dbContext.SaveChanges();
            }
        }

        /// <summary>
        /// 修改信息。
        /// </summary>
        /// <param name="needPayFeeInfo">缴费信息。</param>
        public void Change(NeedPayFeeInfo needPayFeeInfo)
        {
            using (var dbContext = new PatentFeeDbContext())
            {
                var entity = dbContext.NeedPayFeeInfos.FirstOrDefault(p => p.Id == needPayFeeInfo.Id);
                if (entity != null)
                {
                    entity = needPayFeeInfo;
                    dbContext.SaveChanges();
                }
            }
        }

        /// <summary>
        /// 删除信息。
        /// </summary>
        /// <param name="id">缴费信息id。</param>
        public void Remove(int id)
        {
            using (var dbContext = new PatentFeeDbContext())
            {
                var entity = dbContext.NeedPayFeeInfos.FirstOrDefault(p => p.Id == id);
                if (entity != null)
                {
                    dbContext.NeedPayFeeInfos.Remove(entity);
                    dbContext.SaveChanges();
                }
            }
        }

        /// <summary>
        /// 根据id获取信息。
        /// </summary>
        /// <param name="id">缴费id。</param>
        /// <returns>返回对应id的信息，如果没有则返回null。</returns>
        public NeedPayFeeInfo GetNeedPayFeeInfoById(int id)
        {
            using (var dbContext = new PatentFeeDbContext())
            {
                return dbContext.NeedPayFeeInfos.FirstOrDefault(p => p.Id == id);
            }
        }

        /// <summary>
        /// 根据条件查询缴费信息。
        /// </summary>
        /// <param name="applicationNumber">申请号。</param>
        /// <param name="feeType">缴费类型，null为全部。</param>
        /// <param name="deadLineFrom">最后期限。</param>
        /// <param name="deadLineTo">最后期限。</param>
        /// <param name="isPayed">是否已经缴费，null为全部。</param>
        /// <returns>返回对应条件的缴费信息。</returns>
        public List<NeedPayFeeInfo> FindNeedPayFeeInfos(string applicationNumber,
            FeeTypeEnum? feeType, DateTime? deadLineFrom, DateTime? deadLineTo, bool? isPayed)
        {
            using (var dbContext = new PatentFeeDbContext())
            {
                var ret = dbContext.NeedPayFeeInfos.Select(p => p);
                ret = ret.Where(p => p.ApplicationNumber.Contains(applicationNumber));
                if (feeType != null)
                {
                    ret = ret.Where(p => p.FeeType == feeType);
                }
                if (deadLineFrom.HasValue)
                {
                    ret = ret.Where(p => p.DeadLine >= deadLineFrom.Value);
                }
                if (deadLineTo.HasValue)
                {
                    ret = ret.Where(p => p.DeadLine <= deadLineTo.Value);
                }
                if (isPayed.HasValue)
                {
                    ret = ret.Where(p => p.IsPayed == isPayed.HasValue);
                }
                return ret.ToList();
            }
        }
    }
}
